import sys
import json
import time

from math import radians, atan, acos, tan, sin, cos, atan2, asin, sqrt


# input Lat_A 纬度A
# input Lng_A 经度A
# input Lat_B 纬度B
# input Lng_B 经度B
# output distance 距离(km)
def calcDistance(Lat_A, Lng_A, Lat_B, Lng_B):
    ra = 6378.140  # 赤道半径 (km)
    rb = 6356.755  # 极半径 (km)
    flatten = (ra - rb) / ra  # 地球扁率
    rad_lat_A = radians(Lat_A)
    rad_lng_A = radians(Lng_A)
    rad_lat_B = radians(Lat_B)
    rad_lng_B = radians(Lng_B)
    pA = atan(rb / ra * tan(rad_lat_A))
    pB = atan(rb / ra * tan(rad_lat_B))
    xx = acos(sin(pA) * sin(pB) + cos(pA) * cos(pB) * cos(rad_lng_A - rad_lng_B))
    c1 = (sin(xx) - xx) * (sin(pA) + sin(pB)) ** 2 / cos(xx / 2) ** 2
    c2 = (sin(xx) + xx) * (sin(pA) - sin(pB)) ** 2 / sin(xx / 2) ** 2
    dr = flatten / 8 * (c1 - c2)
    distance = ra * (xx + dr)
    return distance

def calcDistance2(lat1, lng1, lat2, lng2):
    radLat1 = radians(lat1)
    radLat2 = radians(lat2)
    a = radLat1 - radLat2
    b = radians(lng1) - radians(lng2)
    s = 2 * asin(sqrt(sin(a / 2) * sin(a / 2) + cos(radLat1) * cos(radLat2)* sin(b / 2) * sin(b / 2)))
    s = s * 6378.137
    return s

def calcDeg(Lat_A, Lng_A, Lat_B, Lng_B):
    rad_lat_A = radians(Lat_A)
    rad_lng_A = radians(Lng_A)
    rad_lat_B = radians(Lat_B)
    rad_lng_B = radians(Lng_B)
    dLon = Lng_B - Lng_A
    y = sin(dLon) * cos(rad_lat_B)
    x = cos(rad_lat_A) * sin(rad_lat_B) - sin(rad_lat_A) * cos(rad_lat_B) * cos(dLon)
    brng = atan2(y, x)
    return brng

# http://www.tageo.com/index-e-ch-airport-ch-lg-ch.htm
ZBAA_lon = 116.580
ZBAA_lat = 40.080

if __name__ == '__main__':
    if len(sys.argv) == 2:
        filename = sys.argv[1]
    else:
        filename = 'result.json'
    with open(filename, 'r', encoding='utf-8') as fin:
        content = json.load(fin)
    result = {}
    for fid in content:
        data = content[fid]
        timestamps = data.keys()
        # timestamps = sorted(timestamps)
        print(timestamps)
        tracks = []
        for ts in timestamps:
            flight_dst = data[ts]['dst']
            if flight_dst != 'PEK':
                continue
            flight_lat = data[ts]['latitude']
            flight_lon = data[ts]['longitude']
            flight_hft = data[ts]['calibrated altitude(ft)']
            flight_kts = data[ts]['ground speed(kts)']
            flight_deg = data[ts]['track']
            
            dist = calcDistance2(ZBAA_lat, ZBAA_lon, flight_lat, flight_lon)
            deg = calcDeg(ZBAA_lat, ZBAA_lon, flight_lat, flight_lon)
            flight_h = flight_hft * 0.3048
            flight_v = flight_kts * 1.852
            res = {
                'x': cos(deg) * dist,
                'y': sin(deg) * dist,
                'h': flight_h,
                'v': flight_v,
                'theta': flight_deg,
                'time': ts
            }
            tracks.append(res)
        
        if len(tracks) == 0:
            continue
        struct = {
            'flightNumber': fid,
            'dst': 'ZBAA',
            'track': tracks
        }
        result[fid] = struct
    with open('dump.json', 'w', encoding='utf-8') as fout:
        json.dump(result, fout)
        



